//
//  ClubBillVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/30.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ClubBillVC.h"
#import "ClubBillCell.h"
#import "ClubBillDetailVC.h"
@interface ClubBillVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ClubBillVC
{
    UITableView *_tableView;
    NSDictionary *_responseDic;
    NSArray *_responseArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LighterGrayColor;
    self.title = @"俱乐部账单";
    [self requestForClubBillList];
    [self initTableView];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
}
- (void)requestForClubBillList{
    //参数
    NSDictionary *parameters = @{
                                 @"clubID":[NSString readToNSDefaultsWithKey:kClubId],
//                                 @"status": @"2",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/clubBillList.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //请求成功
            [SVProgressHUD showSuccessWithStatus:@"请求成功"];
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = superBackgroundColor;
    [self.view addSubview:_tableView];
}
#pragma - mrak UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 96;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ClubBillCell *cell = [[NSBundle mainBundle]loadNibNamed:@"ClubBillCell" owner:nil options:nil][0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = superBackgroundColor;
    //cell的bgView的阴影效果
    cell.bgView.layer.shadowColor = [UIColor grayColor].CGColor;
    cell.bgView.layer.shadowOffset = CGSizeMake(2,2);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    cell.bgView.layer.shadowOpacity = 0.5;
    cell.bgView.layer.shadowRadius = 2;
    
    cell.titleLb.text = _responseArr[indexPath.row][@"activityName"];
    cell.nameLb.text = _responseArr[indexPath.row][@"leaderName"];
    NSNumber *num = _responseArr[indexPath.row][@"activityTotalMoney"];
    cell.moneyLb.text = num.stringValue;
    NSString *timeStr = _responseDic[@"list"][indexPath.row][@"activityBeginTime"];
    timeStr = [timeStr substringToIndex:10];
    cell.timeLb.text = timeStr;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ClubBillDetailVC *vc = [[ClubBillDetailVC alloc]init];
    vc.activityName = _responseArr[indexPath.row][@"activityName"];
    vc.totalMoney = _responseArr[indexPath.row][@"activityTotalMoney"];
    vc.leaderName = _responseArr[indexPath.row][@"leaderName"];
    NSString *timeStr = _responseDic[@"list"][indexPath.row][@"activityBeginTime"];
    timeStr = [timeStr substringToIndex:10];
    vc.beganTime = timeStr;
    NSString *endStr = _responseDic[@"list"][indexPath.row][@"activityEndTime"];
    endStr = [endStr substringToIndex:10];
    vc.endTime = endStr;
    vc.arr = _responseDic[@"list"][indexPath.row][@"billList"];
    [self.navigationController pushViewController:vc animated:YES];
}
//
- (void)searchAction{

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
